/**
 * 系统设置权限设置添加管理员
 */
import './index.less'
import tem from './index.html'
import $_fn from '~plugins'
class gatheringCtrl {
  constructor($scope,$rootScope,http,$ui,$state,$location,$timeout) {
    $scope.param = {}
    $scope.data = {}
    $scope.option = {}
    $scope.changeEle = {}
    $scope.checked = {}
    $scope.err = {}
    $scope.cancel = function () {
        $rootScope.$uiDirectiveModal.close() //关闭方法
    }
    //警告提示
    $scope.errorToast = function(text) {
      $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
    }
    //成功提示
    $scope.successToast = function(text) {
        $ui.toast({text:text,type:'success',position:'center',appendTo:'.wp-container'});
    }
    //
    console.log($scope)
    http.post('selectPaymentSort.do',{},function (data) {
      if(data.errcode==1){
        console.log(data);
        let _data = data.data
        $scope.$apply(function(){
          $scope.param.ps_id = 1
          $scope.option.gatherType = _data
        })
      }else{
        $scope.errorToast(data.errmsg || '请求失败！')
        $scope.cancel()
      }
    })

  	http.post('submitReceivablesInit.do',{order_id:$scope.params.id},function (data) {
  		if(data.errcode==1){
  			console.log(data);
        let _data = data.data
  			$scope.$apply(function(){
          $scope.data = _data
          $scope.param.fund_plan_id = _data.funMap.id

          /*获取数据更新页面*/
          $scope.funPlan = _data.funPlanlList[0];
          $scope.funPlan.now_money = $scope.funPlan.is_month_repayment_money+$scope.funPlan.is_moth_repayment_rate+$scope.funPlan.is_parking;
          console.log('$scope.funPlan.now_money',$scope.funPlan,$scope.funPlan.now_money)
          $scope.param.is_moth_repayment_rate = $scope.funPlan.is_month_repayment_money;
          $scope.param.is_moth_repayment_rate = $scope.funPlan.is_moth_repayment_rate;
          
          if($scope.param.ps_id == 4 ) {
            $scope.changeEle.money = $scope.data.maps.sumMoney;
          }else if($scope.param.ps_id != 4) {
            $scope.changeEle.money = $scope.funPlan.now_money;
          }
          $scope.param.plan_payment_money = $scope.changeEle.money;

          console.log('last11',$scope.data.maps.repayment_way,$scope.data.funPlanlList[0].type)
          //先息后本 不是是最后一期
          if($scope.data.maps.repayment_way ==1 && $scope.data.funPlanlList[0].type !=3 ){
            console.log('last')
            $scope.changeEle.benjin = true
          }
        })
  		}else{
        $scope.errorToast(data.errmsg || '请求失败！')
        $scope.cancel()
  		}
    })
    /*收款归类 切换*/
    $scope.$watch('param.fund_plan_id',function(newVal){
      for(let i=0; i<$scope.data.funPlanlList.length; i++){
        if($scope.data.funPlanlList[i].id==newVal){
          $scope.param.periods=$scope.data.funPlanlList[i].periods
        }
      }
      console.log('fund_plan_id:', newVal, $scope.param)

    })

    /*收款归类 切换*/
    $scope.$watch('param.ps_id',function(newVal){
      console.log('ps_id:', newVal)
      /*正常还款*/
      if($scope.param.ps_id == 1)
      {
        $scope.changeEle.penalty = false
        $scope.param.penalty = 0
        if($scope.data.maps && $scope.data.maps.s_id == 18) {
            $scope.changeEle.late_fee = true
        } else {
            $scope.changeEle.late_fee = false
            $scope.param.late_fee = 0
        }
        //催收管理的提交收款
        if ($scope.params.nowPage == 'serviceCollect'){
          $scope.changeEle.penalty = true
        }
        // $scope.param.plan_payment_money = $scope.changeEle.money
      }
      /*逾期还款*/
      if($scope.param.ps_id == 3)
      {
        $scope.changeEle.penalty = false
        $scope.changeEle.late_fee = true
        $scope.param.penalty = 0
        //催收管理的提交收款
        if ($scope.params.nowPage == 'serviceCollect'){
          $scope.changeEle.penalty = true
        }
      }
      /*坏账*/
      if($scope.param.ps_id == 2)
      {
        $scope.changeEle.penalty = false
        $scope.changeEle.late_fee = false
        $scope.param.penalty = 0
        $scope.param.late_fee = 0
      }
      /*提前结清*/
      $scope.changeEle.loaninfo = true
      if($scope.param.ps_id == 4)
      {
        $scope.changeEle.penalty = true
        $scope.changeEle.late_fee = true
        $scope.changeEle.loaninfo = false
        // $scope.changeEle.qishu = false
        // $scope.changeEle._money = $scope.data.content.maps.sumMoney
        // $scope.changeEle.benjin = true
        // if($scope.changeEle._money < $scope.sum || $scope.sum == 0){
        //     $scope.isDisabled = true
        // }
        // if($scope.param.depositReturn == 1) {
        //     $scope.param.plan_payment_money = $scope.changeEle._money - $scope.sum
        // }else if($scope.param.depositReturn == -1) {
        //     $scope.param.plan_payment_money = $scope.changeEle._money
        // }
        // console.log($scope.changeEle._money)
      }
    })
    //利息不选中时隐藏而且清空  借款信息录入
    $scope.interest = function (param) {
      if (param == 'ben_money') {
          $scope.checked.ben_money  = !$scope.checked.ben_money
      }
      if (param == 'interest') {
        $scope.checked.interest  = !$scope.checked.interest
      }
      if (param == 'penalty') {
        $scope.checked.penalty  = !$scope.checked.penalty
      }
      if (param == 'late_fee') {
        $scope.checked.late_fee  = !$scope.checked.late_fee
      }
    }
    $scope.ok = function(){
      $scope.param.oid = $scope.params.id
      $scope.param.status = 1   //待复核
      console.log('dormData:',$scope.param)
      if ($scope.param.real_payment_money ==null ||  $scope.param.real_payment_money==undefined) {
        $scope.err.real_payment_money = true
        $scope.errorToast('请填写到账总额')
        return 
      }
      if (!$scope.param.real_time) {
        $scope.err.real_time = true
        $scope.errorToast('请选择到账日期')
        return 
      }
      if ($scope.param.remarksPeriods ==null ||  $scope.param.remarksPeriods==undefined) {
        $scope.err.remarksPeriods = true
        $scope.errorToast('请填写备注')
        return 
      }

      http.post('submitReceivables.do', $scope.param, function (data) {
        if(data.type==1){
          console.log(data);
          $scope.successToast(data.errmsg)
          $scope.cancel()

          $timeout(function(){
            window.location.reload()
          },1000)
        }else{
          $scope.errorToast(data.errmsg || '请求失败！')
        }
      })
    }

    $scope.remlight = function () {
      for (var i in $scope.err) {
        $scope.err[i] = false
      }
    }
  }
}
gatheringCtrl.$inject = ['$scope','$rootScope','http','$ui','$state','$location','$timeout','$interval']
module.exports = angular.module('directive.gathering', []).directive('gathering', function() {
  return {
    template: tem,
    replace:true,
    controller: gatheringCtrl,
    scope:{
    	params:"<"
    },
    link:function(){
      $_fn.clickBind()
    }
  }
}).name